package kr.hkit.androidcomplete03.ch20;

import kr.hkit.androidcomplete03.R;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MultiPaneWidth extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch20_multipanewidth);
	}
	
	public static class PlanetListFragment extends ListFragment{
		boolean mMultiPane;
		int mLastIndex = 0;
		
		@Override
		public void onActivityCreated(Bundle savedInstanceState) {
			// TODO Auto-generated method stub
			super.onActivityCreated(savedInstanceState);
		
			setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, 
					PlanetInfo.PLANET));
			getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
			
			if(savedInstanceState !=null){
				
				onListItemClick(getListView(), null, mLastIndex, 0);
			}
			
			View descPanel = getActivity().findViewById(R.id.ch20_descfragment_tv);
			if(descPanel != null && descPanel.getVisibility() == View.VISIBLE){
				mMultiPane = true;
				onListItemClick(getListView(), null, mLastIndex, 0);
			}
		}
		
		@Override
		public void onSaveInstanceState(Bundle outState) {
			// TODO Auto-generated method stub
			super.onSaveInstanceState(outState);
			outState.putInt("lastindex", mLastIndex);
		}
		
		@Override
		public void onListItemClick(ListView l, View v, int position, long id) {
			mLastIndex = position;
			getListView().setItemChecked(position, true);
			
			//태블릿일 경우
			if(mMultiPane){
				FragmentManager FM = getFragmentManager();
				DescFragment DF = (DescFragment) FM.findFragmentById(R.id.ch20_multipanewidth_desc);
				
				if(DF == null || DF.mNowIndex != position){
					FragmentTransaction TR = FM.beginTransaction();
					TR.replace(R.id.ch20_multipanewidth_desc, DF);
					TR.commit();
				}
			//핸드셋
			}else{
				Intent intent = new Intent(getActivity(), DescActivity.class);
				intent.putExtra("index", position);
				startActivity(intent);
			}
		}
	}
	
	//설명을 보여주는---- 프래그먼트-----
	public static class DescFragment extends Fragment{
		int mNowIndex;
		
		public static DescFragment newInstance(int index){
			DescFragment DF = new DescFragment();
			DF.mNowIndex = index;
			return DF;
		}
		
		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			// TODO Auto-generated method stub
			View root = inflater.inflate(R.layout.ch20_descfragment, container, false);//여기 해야함
			TextView tv = (TextView) root.findViewById(R.id.ch20_descfragment_tv);
			tv.setText(PlanetInfo.DESC[mNowIndex]);
			return root;
		}
	}
	
	//설명을 보여주는---액티비티----
	public static class DescActivity extends Activity{
		@Override
		protected void onCreate(Bundle savedInstanceState) {
			// TODO Auto-generated method stub
			super.onCreate(savedInstanceState);
			int index = getIntent().getExtras().getInt("index");
			DescFragment details = DescFragment.newInstance(index);
			getFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
		}
	}
}
